/*
 * Copyright 2018 Dash Core Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * This file was generated by SWIG (http://www.swig.org) and modified.
 * Version 3.0.12
 */
package org.dashj.bls;

import com.google.common.base.Preconditions;

public class ChainCode extends BLSObject {

  protected ChainCode(long cPtr, boolean cMemoryOwn) {
    super(cPtr, cMemoryOwn);
  }

  protected static long getCPtr(ChainCode obj) {
    return (obj == null) ? 0 : obj.cPointer;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    JNI.delete_ChainCode(cPointer);
  }

  public static ChainCode FromBytes(byte[] bytes) {
    Preconditions.checkNotNull(bytes);
    Preconditions.checkArgument(bytes.length == CHAIN_CODE_SIZE);
    return new ChainCode(JNI.ChainCode_FromBytes(bytes), true);
  }

  public ChainCode(ChainCode cc) {
    this(JNI.new_ChainCode(ChainCode.getCPtr(cc), cc), true);
  }

  public void Serialize(byte[] buffer) {
    Preconditions.checkNotNull(buffer);
    Preconditions.checkArgument(buffer.length >= CHAIN_CODE_SIZE);
    JNI.ChainCode_Serialize__SWIG_0(cPointer, this, buffer);
  }

  public byte [] Serialize() {
    byte [] bytes = new byte[(int)CHAIN_CODE_SIZE];
    Serialize(bytes);
    return bytes;
  }

  @Override
  public String toString() {
    return "ChainCode(" + Utils.HEX.encode(Serialize()) + ")";
  }

  public final static long CHAIN_CODE_SIZE = JNI.ChainCode_CHAIN_CODE_SIZE_get();
}
